<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="图片标题" prop="picTitle">
        <el-input
          v-model="queryParams.picTitle"
          placeholder="请输入图片标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>

        <el-button
          type="danger"
          plain
          icon="el-icon-plus"
          size="mini"
          v-if="viewType=='selectpro'"
          @click="confirmSelect"
        >确定选择</el-button>
      </el-form-item>


    </el-form>

<!--    <el-row :gutter="10" class="mb8">-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['business:pictures:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['business:pictures:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['business:pictures:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->



    <el-table v-loading="loading" :data="picturesList" @selection-change="handleSelectionChange"  @row-click="singleElection">
      <el-table-column label="序号" width="65" v-if="viewType=='selectpro'">
        <template slot-scope="scope">
          <el-radio
            class="radio"
            v-model="templateSelection"
            :label="scope.$index + 1"
            :value="scope.row.id"
            tabindex="0"
          >
          </el-radio>
        </template>
      </el-table-column>
      <el-table-column type="selection"   v-if="viewType!='selectpro'" width="55" align="center" />
<!--      <el-table-column label="主键自增" align="center" prop="id" />-->
      <el-table-column label="图片标题" align="center" prop="picTitle"  width="300"/>
<!--      <el-table-column label="关键词" align="center" prop="picDesc" />-->
<!--      <el-table-column label="文件业务类型" align="center" prop="picSuffix" />-->
      <el-table-column label="存储地址" align="center" prop="s3Url"  >
        <template slot-scope="scope">
          <image-preview :src="previewUrl+scope.row.s3Url" :width="100" :height="100"/>
        </template>
      </el-table-column>


<!--      <el-table-column label="来源网页" align="center" prop="pageUrl" />-->
<!--      <el-table-column label="ocr" align="center" prop="ocrLabels" />-->
<!--      <el-table-column label="发布时间" align="center" prop="publishTime" width="180">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.publishTime, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="数据来源编号" align="center" prop="sourceId" />-->
<!--      <el-table-column label="数据来源名称" align="center" prop="sourceName" />-->
<!--      <el-table-column label="入库时间" align="center" prop="insertTime" width="180">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.insertTime, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--        <template slot-scope="scope">-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['business:pictures:edit']"-->
<!--          >修改</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['business:pictures:remove']"-->
<!--          >删除</el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { listPictures, getPictures, delPictures, addPictures, updatePictures } from "@/api/business/pictures";

export default {
  props: {
    viewType: {
      type: String,
      default: null
    }
  },
  name: "Pictures",
  data() {
    return {

      previewUrl: process.env.VUE_APP_API_BASE_URL,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 图片库表格数据
      picturesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 5,
        picId: null,
        picTitle: null,
        picDesc: null,
        picSuffix: null,
        s3Url: null,
        pageUrl: null,
        ocrLabels: null,
        publishTime: null,
        sourceId: null,
        sourceName: null,
        insertTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      templateSelection:null,
      selectRow:null,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    singleElection (row) {
      this.selectRow=row
      this.templateSelection = this.picturesList.indexOf(row)+1;
      console.log(this.templateSelection+"mmmmmmmmmmmmmmm")
      this.ids= row.id;
    },

    confirmSelect() {
      if(!this.selectRow){
        this.$modal.msgError("请选择一条记录");
        return
      }
      // 传递个父组件
      this.$emit('selectJlProduct',this.selectRow)

    },

    /** 查询图片库列表 */
    getList() {
      this.loading = true;
      listPictures(this.queryParams).then(response => {
        this.picturesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        picId: null,
        picTitle: null,
        picDesc: null,
        picSuffix: null,
        s3Url: null,
        pageUrl: null,
        ocrLabels: null,
        publishTime: null,
        sourceId: null,
        sourceName: null,
        insertTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加图片库";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPictures(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改图片库";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePictures(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPictures(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除图片库编号为"' + ids + '"的数据项？').then(function() {
        return delPictures(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/pictures/export', {
        ...this.queryParams
      }, `pictures_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
